﻿using System;
using System.Linq;
using System.Web.Mvc;
using Diploma.Models;
using Diploma.Validation;

namespace DiplomaWeb.Controllers
{
    [Authorize]
    public class GlobalTasksController : MasterController
    {
        public GlobalTasksController(DataManager dataManager) : base(dataManager)
        {
        }

        public ActionResult View(Guid id)
        {
            GlobalTask task = DataManager.GlobalTasksRepository.GetGlobalTask(id);
            if (task == null)
                throw new Exception();

            return PartialView("~/Views/SystemManagers/Planings/GlobalTaskViewControl.cshtml", task);
        }

        [HttpGet]
        public ActionResult Add()
        {
            ViewData["Levels"] = new SelectList(DataManager.QualificationLevelsRepository.GetAllLevels(),
                                                "QualificationLevelId", "");
            ViewData["PlaningTypes"] = new SelectList(DataManager.PlaningTypesRepository.GetAllPlaningTypes(),
                                                      "PlaningTypeId", "");

            return PartialView("~/Views/SystemManagers/Planings/GlobalTaskAddControl.ascx");
        }

        [HttpPost]
        public JsonResult Add(GlobalTask globalTask)
        {
            ValidationErrors = Validator.ValidateGlobalTask(globalTask);
            if (ValidationErrors.Count() == 0)
            {
                DataManager.GlobalTasksRepository.Add(globalTask);
            }

            return Json(ValidationErrors, "text/plain", JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            GlobalTask task = DataManager.GlobalTasksRepository.GetGlobalTask(id);
            if (task == null)
                throw new Exception();
            ViewData["Levels"] = new SelectList(DataManager.QualificationLevelsRepository.GetAllLevels(),
                                                "QualificationLevelId", "", task.QualificationLevelId);
            ViewData["PlaningTypes"] = new SelectList(DataManager.PlaningTypesRepository.GetAllPlaningTypes(),
                                                      "PlaningTypeId", "", task.PlaningTypeId);

            return PartialView("~/Views/SystemManagers/Planings/GlobalTaskEditControl.ascx", task);
        }

        [HttpPost]
        public JsonResult Edit(GlobalTask globalTask)
        {
            ValidationErrors = Validator.ValidateGlobalTask(globalTask);
            if (ValidationErrors.Count() == 0)
            {
                DataManager.GlobalTasksRepository.Edit(globalTask);
            }

            return Json(ValidationErrors, "text/plain", JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GlobalTaskStatusEdit(Guid globalTaskId, Guid studentId)
        {
            GlobalTask globalTask = DataManager.GlobalTasksRepository.GetGlobalTask(globalTaskId);
            Student student = DataManager.StudentsRepository.GetStudent(studentId);
            GlobalTasksCompletion gtCompletion = DataManager.GlobalTasksCompletionsRepository.GetGlobalTaskCompletion(globalTask,
                                                                                                     student);
            ViewData["Student"] = student;
            if (globalTask == null)
                throw new Exception();

            ViewData["Levels"] = new SelectList(DataManager.QualificationLevelsRepository.GetAllLevels(),
                                                "QualificationLevelId", "", globalTask.QualificationLevelId);
            ViewData["PlaningTypes"] = new SelectList(DataManager.PlaningTypesRepository.GetAllPlaningTypes(),
                                                      "PlaningTypeId", "", globalTask.PlaningTypeId);
            GlobalTask nextTask = DataManager.GlobalTasksRepository.GetNextTasks(globalTask).FirstOrDefault();
            GlobalTask prevTask = (globalTask.PrevCheckPoint == null) ? null : globalTask.PrevCheckPoint as GlobalTask;

            ViewData["ThisTaskCompletion"] = DataManager.GlobalTasksCompletionsRepository.GetCompletion(globalTask,
                                                                                                      student);
          
            ViewData["PreviousTaskCompletion"] = (prevTask == null)
                                                     ? null
                                                     : DataManager.GlobalTasksCompletionsRepository.GetCompletion(
                                                       prevTask, student);
            ViewData["NextTaskCompletion"] = (nextTask == null)
                                                ? null
                                                : DataManager.GlobalTasksCompletionsRepository.GetCompletion(nextTask,
                                                                                                            student);
            if (gtCompletion != null)
            {
                ViewData["CompletionDate"] = gtCompletion.CompletionDate;
            }
            return PartialView("~/Views/Lecturers/Tasks/GlobalTaskStatusEditControl.ascx", globalTask);
        }

        [HttpPost]
        public ActionResult GlobalTaskStatusEdit(Guid globalTaskId, Guid studentId, bool? isCompleted)
        {
            GlobalTask globalTask = DataManager.GlobalTasksRepository.GetGlobalTask(globalTaskId);
            Student student = DataManager.StudentsRepository.GetStudent(studentId);
            if (globalTask == null || student == null)
                throw new Exception();
            DataManager.GlobalTasksRepository.SetCompletion(globalTask, student, isCompleted);
            return new EmptyResult();
        }

        public ActionResult Delete(Guid id)
        {
            GlobalTask task = DataManager.GlobalTasksRepository.GetGlobalTask(id);
            if (task == null)
                throw new Exception();
            DataManager.GlobalTasksRepository.Delete(task);
            return new EmptyResult();
        }

        public JsonResult ByLevelAndPlaningJSON(int levelId, int planingId, string date)
        {
            DateTime selectedDate = DateTime.Parse(date);
            return
                Json(
                    from c in
                        DataManager.GlobalTasksRepository.GetGlobalTaskByLevelAndPlaningType(levelId, planingId,
                                                                                             selectedDate)
                    select new {id = c.CheckPointId, name = c.Name}, JsonRequestBehavior.AllowGet);
        }
    }
}